﻿using AutoMapper;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProdService
{
    internal class MapConfig
    {
        internal static Action<IMapperConfigurationExpression> get()
        {
            return cfg =>
            {
                ///Note：
                ///1、请按entity组织方法，
                ///2、CreateMap()时始终是entity在前，ViewModel在后
                onUser(cfg);
                onContact(cfg);
                onPhone(cfg);
                onCareer(cfg);
            };
        }

        private static void onCareer(IMapperConfigurationExpression cfg)
        {
            cfg.CreateMap<Career, ViewModels.Career.NeedHelpModel>(MemberList.None)
                .ReverseMap()
                ;
            cfg.CreateMap<Career, ViewModels.Career._NeedHelpItemModel>(MemberList.None)
                .ForPath(n => n.Author.UserId, opt => opt.MapFrom(c => c.User.Id))
                .ForPath(n => n.Author.NickName, opt => opt.MapFrom(c => c.User.NickName))
                .ReverseMap()
                ;
        }

        private static void onPhone(IMapperConfigurationExpression cfg)
        {
            //TODO：调整顺序
            cfg.CreateMap<ViewModels.User.RegisterModel, Phone>(MemberList.None)
                ;
            cfg.CreateMap<ViewModels.Shared.PhoneModel, Phone>(MemberList.None)
                .ForMember(p => p.Number, opt => opt.MapFrom(r => r.Number))
                .ReverseMap();
            ;
        }

        private static void onContact(IMapperConfigurationExpression cfg)
        {
            cfg.CreateMap<Contact, ViewModels.Contact.EditModel>(MemberList.None)
                .ReverseMap()
                ;
            cfg.CreateMap<Contact, ViewModels.Contact.ShowModel>(MemberList.None)
                .ReverseMap()
                ;
        }

        private static void onUser(IMapperConfigurationExpression cfg)
        {
            //TODO：调整顺序
            cfg.CreateMap<ViewModels.User.RegisterModel, User>(MemberList.None)
                .ForMember(u => u.Password, opt => opt.MapFrom(r => r.Password))
                .ReverseMap()
                ;

            cfg.CreateMap<User, ViewModels.User._InviteCodeModel>(MemberList.None)
                .ForMember(i => i.UserId, opt => opt.MapFrom(u => u.Id))
                .ReverseMap()
                ;

            cfg.CreateMap<User, ViewModels.Components.NavigatorModel>(MemberList.None)
                .ForMember(n => n.UserId, opt => opt.MapFrom(u => u.Id))
                ;

            cfg.CreateMap<User, ViewModels.User.NameModel>(MemberList.None)
                .ReverseMap()
                ;
        }
    }
}
